﻿namespace MelodysDemo.Application.Users
{
    public class UserService : IUserService
    {
        readonly IRepository<SurveyUser, int> _surveyUser;
        public UserService(IRepository<SurveyUser, int> surveyUser)
        {
            _surveyUser = surveyUser;
        }
        public async Task<PagedResultDto<SurveyUser>> GetResultDto(UserInput input)
        {
            var q = _surveyUser.GetAll().Where(u => u.IsDelete == 0);

            //姓名
            if (input.Username is { Length: > 0 })
            {
                q = q.Where(u => u.Username.Contains(input.Username));
            }
            //手机号
            if (input.Phone is { Length: > 0 })
            {
                q = q.Where(u => u.Phone.Contains(input.Phone));
            }
            //状态
            if (input.Status < 2)
            {
                q = q.Where(u => u.Status == input.Status);
            }

            var count = q.Count();
            var list = await q.OrderByDescending(x => x.Id)
                                               .Skip((input.Page - 1) * input.Limit).Take(input.Limit)
                                               .AsNoTracking().ToListAsync();
            return new PagedResultDto<SurveyUser>
            {
                Page = input.Page,
                Limit = input.Limit,
                TotalCount = count,
                Data = list
            };
        }

    }
}
